package com.starter.home.controller;


import com.starter.home.domain.entity.Friends;
import com.starter.home.domain.entity.User;
import com.starter.home.domain.vo.FriendVO;
import com.starter.home.service.IFriendsService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.MyUtil;
import com.starter.home.util.PageUtil;
import com.starter.home.util.ResponseResult;
import com.starter.home.util.constant.MessageConstant;
import com.starter.home.util.constant.UserConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-05-04
 */
@RestController
@RequestMapping("/friends")
public class FriendsController {
    @Autowired
    private IFriendsService friendsService;

    @GetMapping(value="/getFriends")
    public ResponseResult getFriends(@ModelAttribute CommonParam commonParam, HttpSession session){
        User user = (User) session.getAttribute("user");

        if ( user != null ){
            commonParam.setUserId(user.getId());
        }
        PageUtil friends = friendsService.getFriends(commonParam);
        return ResponseResult.success(friends);
    }
    @GetMapping(value="/getFriendsApply")
    public ResponseResult getFriendsApply(HttpSession session){
        User user = (User) session.getAttribute("user");

        /*if (StringUtils.equals(UserConst.ADMIN,user.getAccount())){
            return null;
        }*/
        if (user!=null){
            List<FriendVO> friends = friendsService.getFriendsApply(user.getId());
            return ResponseResult.success(friends);
        }else{
            return ResponseResult.error("错误");
        }
    }

    @PostMapping(value="/saveRemark")
    @ResponseBody
    public ResponseResult saveRemark(@RequestBody Friends friends,HttpSession session){
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            int result = friendsService.saveRemark(user.get().getId(),friends);
            return result > 0 ? ResponseResult.success() : ResponseResult.error(MessageConstant.SAVE_ERROR);
        }else {
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }
    @PostMapping(value="/saveFriend")
    @ResponseBody
    public ResponseResult saveFriend(@RequestBody Friends friends,HttpSession session){
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            friends.setFromId(user.get().getId());
            friends.setGroupId(1);
            friends.setStatus(1);
            int result = friendsService.getBaseMapper().insert(friends);
            return result > 0 ? ResponseResult.success() : ResponseResult.error(MessageConstant.SAVE_ERROR);
        }else {
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }

    @GetMapping(value="/{id}/{status}")
    @ResponseBody
    public ResponseResult changeStatus(@PathVariable Integer id,@PathVariable Integer status){
        boolean result = false;
        switch (status){
            case 4 :
                result = friendsService.changStatus(id,status);
                break;
            case 0:
                result = friendsService.deleteFriend(id,status);
                break;
            case 3 :
                result = friendsService.changStatus(id,status);
                break;
            case 2 :
                result = friendsService.changStatus(id,status);
                break;
        }

        return result ? ResponseResult.success():ResponseResult.error("失败");
    }

    @GetMapping(value="/add/{id}")
    @ResponseBody
    public ResponseResult add(@PathVariable Integer id){
        Friends friends = friendsService.getBaseMapper().selectById(id);
        friends.setStatus(2);

        Friends friends1 = new Friends();
        friends1.setGroupId(1);
        friends1.setStatus(2);
        friends1.setFromId(friends.getToId());
        friends1.setToId(friends.getFromId());
        friendsService.getBaseMapper().updateById(friends);
        int insert = friendsService.getBaseMapper().insert(friends1);
        return insert > 0 ? ResponseResult.success():ResponseResult.error("失败");
    }
}
